package main

import (
    "fmt"
    "os"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "context"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
    kubeconfig := os.Getenv("HOME") + "/.kube/config"  // 使用当前用户的 kubeconfig 文件配置 Kubernetes 客户端
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        panic(err.Error())
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
    fmt.Println("Listing pods with their IPs:")
    for _, pod := range pods.Items {
        fmt.Printf("%s\t%s\t%s\n", pod.Status.PodIP, pod.Namespace, pod.Name)
    }
}

